//
//  WebViewController.m
//  Ranging_dev
//
//  Created by Mr.Dynasty on 2019/4/1.
//  Copyright © 2019 Mr.Dynasty. All rights reserved.
//

#import "WebViewController.h"
#import <WebKit/WebKit.h>
@interface WebViewController ()

@property (strong, nonatomic) WKWebView *web;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self makeView];
    [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
}

- (void)makeView {
    self.web = [[WKWebView alloc] init];
    [self.view addSubview:self.web];
    [self.web mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view).offset(UIEdgeInsetsMake(STAUEBAR_HEIGHT, 0, TABBAR_HEIGHT, 0));
    }];
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(TABBAR_HEIGHT);
    }];
    
    UIButton *backBtn = [self makeButton:@"上一页"];
    [backBtn addTarget:self action:@selector(actionForGoBack) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(bottomView).offset(UIEdgeInsetsMake(0, 0, kBottomMargin, 0));
        make.width.equalTo(SCREEN_WIDTH / 4.0);
    }];
    
    
    UIButton *forwardBtn = [self makeButton:@"下一页"];
    [forwardBtn addTarget:self action:@selector(actionForGoForword) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:forwardBtn];
    [forwardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(bottomView).offset(UIEdgeInsetsMake(0, 0, kBottomMargin, 0));
        make.left.equalTo(backBtn.mas_right);
        make.width.equalTo(SCREEN_WIDTH / 4.0);
    }];
    
    UIButton *refreshBtn = [self makeButton:@"刷新"];
    [refreshBtn addTarget:self action:@selector(actionForRefresh) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:refreshBtn];
    [refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(bottomView).offset(UIEdgeInsetsMake(0, 0, kBottomMargin, 0));
        make.left.equalTo(forwardBtn.mas_right);
        make.width.equalTo(SCREEN_WIDTH / 4.0);
    }];
    
    UIButton *homeBtn = [self makeButton:@"主页"];
    [homeBtn addTarget:self action:@selector(actionForGoHome) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:homeBtn];
    [homeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(bottomView).offset(UIEdgeInsetsMake(0, 0, kBottomMargin, 0));
        make.left.equalTo(refreshBtn.mas_right);
        make.width.equalTo(SCREEN_WIDTH / 4.0);
    }];
    
}

- (UIButton *)makeButton:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = RegularDefaultFont;
    [button setTitleColor:TextBlackColor forState:UIControlStateNormal];
    return button;
}

- (void)actionForGoBack {
    if (self.web.canGoBack) {
        [self.web goBack];
    }
}

- (void)actionForGoForword {
    if (self.web.canGoForward) {
        [self.web goForward];
    }
}

- (void)actionForRefresh {
    [self.web reloadFromOrigin];
}

- (void)actionForGoHome {
    [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
